/* d_tests.do ******************************************************************

	Conduct basic analysis of MCAS and SAT outcomes. Estimates outputted here are
	used in:
	 - Table 5 (SAT)
	 - Table 6 (MCAS)

******************************************************************************/


use "$stata_data_analysis/final_sample.dta", clear
order *, sequential
rename total_mcas total_mcas_sim
forval i = 1/2 {
local final_controls hispanic female black white sp_biling
local test_scores has_sat_took_10 qt_* sat_reason satvrecn satmrecn satw sattot c_sattot c_satw c_satvrecn ///
	std_scaled_e3-std_scaled_e8 std_scaled_m4-std_scaled_m8 std_scaled_m10 std_scaled_e10 mcas_avge mcas_avgm ///
	std_e3-std_e8 std_e10 std_m3-std_m8 std_m10 total_mcas_e total_mcas_m total_mcas_sim
*Conducts subgroup analysis on test scores for K1
if `i' == 1 {
	local endog			att
	local instrument 		soffany
	local controls			"i.yr_asgn"
	local pscore			"pformany"
	local outcomes			`test_scores'
	local restriction		"in_sims & in_K1"
	local suffix			"an_fr_K1"
	local demographic_list	"hisp asian black white fem male adv urm not_frpl frpl inc1 inc2 inc3 inc4"
}

*Conducts full control analysis on test scores for K1
if `i' == 2 {
	local endog			att
	local instrument 	soffany
	local controls		"i.yr_asgn `final_controls'"
	local pscore		"pformany"
	local outcomes		`test_scores'
	local restriction	"in_sims & in_K1"
	local suffix		"an_fr_K1_cntrl"
	local demographic_list	""
}

	preserve
	get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	outcomes(`outcomes') other_controls(`controls') restriction(`restriction') pscore(`pscore') ///
	outfile("$results/tables/test_`suffix'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
	restore


foreach demo of local demographic_list {
	if "`demo'" == "hisp" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & hispanic == 1"
	}
	if "`demo'" == "asian" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & asian == 1"
	}
	if "`demo'" == "black" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & black == 1"
	}
	if "`demo'" == "fem" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & female == 1"
	}
	if "`demo'" == "male" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & female == 0"
	}
	if "`demo'" == "adv" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & (white == 1 | asian == 1)"
	}
	if "`demo'" == "urm" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & (black == 1 | hispanic == 1)"
	}
	if "`demo'" == "white" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & white == 1"
	}
	if "`demo'" == "frpl" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & frpl == 1"
	}
	if "`demo'" == "not_frpl" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & not_frpl == 1"
	}
	if inlist("`demo'","inc1", "inc2", "inc3", "inc4") {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & `demo' == 1"
	}


	if `i' ==1 {
		preserve
		get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
		outcomes(`outcomes') other_controls(`controls') restriction(`restriction_new') pscore(`pscore') ///
		outfile("$results/tables/test_`suffix_new'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
		restore
	}

}

}

/********************** Wide Reshape for MCAS (for stacked sample) ************************/

use "$stata_data_analysis/final_sample.dta", clear
order *, sequential

keep studentno att in_sims in_K1 soffany yr_asgn hispanic asian female black white ///
	std_scaled_e3-std_scaled_e8 ///
	std_scaled_e10 std_scaled_m3-std_scaled_m8 std_scaled_m10 ELC soff* pform* ///
	frpl not_frpl nLC sp_biling inc1 inc2 inc3 inc4  ///
	std_e3-std_e8 std_e10 std_m3-std_m8 std_m10

reshape long std_scaled_e std_scaled_m std_e std_m, i(studentno) j(grade)


forval i = 1/2 {
local mcas_outcomes std_scaled_e std_scaled_m std_e std_m

*Conducts MCAS analysis with full controls.
if `i' == 1 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn `final_controls' sp_biling"
	local pscore			"pformany"
	local outcomes			`mcas_outcomes'
	local restriction		"in_sims & in_K1"
	local suffix			"an_fr_K1_mcas_cntrl"
	local demographic_list	""
}

*Conduct subgropu analysis for mcas
if `i' == 2 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn"
	local pscore			"pformany"
	local outcomes			`mcas_outcomes'
	local restriction		"in_sims & in_K1"
	local suffix			"an_fr_K1_mcas"
	local demographic_list	"hisp asian black white fem male adv urm not_frpl frpl inc1 inc2 inc3 inc4"
}


	preserve
	get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	outcomes(`outcomes') other_controls(`controls') pscore(`pscore') restriction(`restriction') cluster(studentno) ///
	outfile("$results/tables/mcas`suffix'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
	restore

	* Sub-group analysis
	foreach demo of local demographic_list {
	if "`demo'" == "hisp" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & hispanic == 1"
	}
	if "`demo'" == "asian" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & asian == 1"
	}
	if "`demo'" == "black" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & black == 1"
	}
	if "`demo'" == "fem" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & female == 1"
	}
	if "`demo'" == "male" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & female == 0"
	}
	if "`demo'" == "adv" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & (white == 1 | asian == 1)"
	}
	if "`demo'" == "urm" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & white != 1 & asian != 1"
	}
	if "`demo'" == "white" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & white == 1"
	}
	if "`demo'" == "frpl" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & frpl == 1"
	}
	if "`demo'" == "not_frpl" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & not_frpl == 1"
	}
	if inlist("`demo'","inc1", "inc2", "inc3", "inc4") {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & `demo' == 1"
	}


	preserve
		get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
		outcomes(`outcomes') other_controls(`controls') restriction(`restriction_new') pscore(`pscore') ///
		outfile("$results/tables/mcas`suffix_new'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
	restore

}


}
